<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/19  14:05
// +----------------------------------------------------------------------
namespace Wechat\Controller;

//首页
class IndexController extends BaseController
{
    public function index()
    {
        $Banner=M("Banner");
        $dpic=$Banner->order("sort")->select();
        $this->assign("dpic",$dpic);
//        S('Index_index_booklists',null);
        $booklists=S('Index_index_booklists');

        if(empty($booklists)){
            $booklists=$this->getBookLists();
            S('Index_index_booklists',$booklists,1800);
        }
//        echo "<pre>";
//        var_dump($booklists);exit;

        $this->assign('booklists',$booklists);
        $this->display();
    }

    function getBookLists(){
        //分类
        $Typebook=M("View_book_has_type");
        $field="a.book_id,a.sm,a.dj,a.b_rec,a.b_logo,b.rented_num,c.apprise_num,d.like_num,press";

        $randsead=strtotime(date('Y-m-d')); //随机数种子

        $limit='0,8';
        $order='rand('.$randsead.')';  //随机排序,以天为周期,同一天随机数种子一致
        $order="a.sort desc";

        $type=M('Book_type')->where(['is_recommend'=>'1'])->select();
        foreach($type as $k=>$v){
            $type_id=$v['type_id'];
            $dt=$Typebook->alias("a")
                ->where(["is_del"=>0,'stock_rent'=>['gt','2'],"type_id"=>$type_id])
                ->join("view_book_rented_num b ON a.book_id=b.book_id", "LEFT")
                ->join("view_book_apprise_num c ON a.book_id=c.book_id", "LEFT")
                ->join("view_book_like_num d ON a.book_id=d.book_id", "LEFT")
                ->order($order)
                ->limit($limit)
                ->field($field)
                ->select();


            if(count($dt)%2 == 1 ){
                array_pop($dt);
            }
            foreach ($dt as $kk => $vv) {
                $dt[$kk]['dj'] = number_format(round($dt[$kk]['dj'], 2), 2);
                $dt[$kk]['detail_url'] = U("Book/detail", "id=" . $dt[$kk]['book_id']);
                if ($dt[$kk]['rented_num'] == NULL) {
                    $dt[$kk]['rented_num'] = 0;
                }
                if ($dt[$kk]['apprise_num'] == NULL) {
                    $dt[$kk]['apprise_num'] = 0;
                }
                if ($dt[$kk]['like_num'] == NULL) {
                    $dt[$kk]['like_num'] = 0;
                }
            }
            $type[$k]['booklist']=$dt;
        }
        return $type;
    }
}